package com.cgsd.heyu.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.library.utils.SharedPreferencesHelper;
import com.android.library.utils.StringUtils;
import com.cgsd.heyu.R;
import com.cgsd.heyu.common.Constants;
import com.cgsd.heyu.common.UserModeConfig;

/**
 * @author xiaoxing
 * Description 新用户奖励
 * CreateData: 2020/11/3
 */
public class NewUserDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private TextView tvNum;
    private String num;

    public NewUserDialog(@NonNull Context context) {
        super(context, R.style.dialog_custom);
        this.mContext = context;
        setContentView(R.layout.dialog_new_user);
        Window window = this.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        setCanceledOnTouchOutside(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 获取屏幕宽、高用
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        // 宽度设置为屏幕的0.7
        lp.width = (int) (d.widthPixels);
        dialogWindow.setAttributes(lp);
        initView();
    }

    private void initView() {
        tvNum = findViewById(R.id.tv_num);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
    }

    public NewUserDialog setTvNum(String num){
        this.num = num;
        tvNum.setText(num);
        return this;
    }

    @Override
    public void show() {
        if (StringUtils.isNotEmpty(num) && UserModeConfig.isRegister()){
            super.show();
            //显示过后设置为老用户
            SharedPreferencesHelper.put(Constants.ShareKeyValue.NEW_USER, "1");
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_confirm){
            dismiss();
        }
    }
}
